﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.IO.Ports;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using static System.Collections.Specialized.BitVector32;

namespace custom
{
    internal class ControlGenerator
    {
        private Form1 form;
        private TabControl tabControl;

        public ControlGenerator(Form1 form,TabControl tabControl)
        {
            this.form = form;
            this.tabControl = tabControl;
        }
        private List<string[]> GetConfigValues()
        {
            string exePath = Assembly.GetExecutingAssembly().Location; //获取exe程序文件路径
            string path = Path.Combine(Path.GetDirectoryName(exePath), "conf.ini"); //获取conf.ini文件路径
            List<string[]> values = new List<string[]>();

            if (File.Exists(path))
            {
                string[] lines = File.ReadAllLines(path);

                string section = "";
                string sectionPath = "";
                string sectionCommand = "";
                string close = "";
                foreach (string line in lines)
                {
                    if (line.StartsWith("["))
                    {
                        section = line.Substring(1, line.Length - 2);
                    }
                    else
                    {
                        string[] parts = line.Split(new char[] { '=' }, 2);
                        if ("path".Equals(parts[0]))
                        {
                            sectionPath = parts[1];
                        }
                        if ("command".Equals(parts[0]))
                        {
                            sectionCommand = parts[1];
                        }
                        if ("close".Equals(parts[0]))
                        {
                            close = parts[1];
                            string[] value = new string[] { section, sectionPath, sectionCommand, close };
                            values.Add(value);
                        }

                    }
                    
                }
            }
            return values;
        }
        public void genner()
        {
            List<string[]> values = GetConfigValues();
            //foreach (string[] value in values)
            //{
                //Console.WriteLine($"{value[0]}==={value[1]}==={value[2]}==={value[3]}");
                //Console.WriteLine("==================================");
            //}
            int x = 12;
            int y = 50;
            foreach (string[] value in values)
            {
                //添加tabPage
                TabPage newTabPage = new TabPage();
                newTabPage.Text = value[0];
                tabControl.TabPages.Add(newTabPage);
                //在TabPage中添加一个TextBox控件
                TextBox newTextBox = new TextBox();
                newTextBox.Name = value[0];
                newTextBox.Width = 200;
                newTextBox.Height = 100;
                newTextBox.Multiline = true;
                newTextBox.ScrollBars = ScrollBars.Vertical;
                newTextBox.Dock = DockStyle.Fill;
                newTabPage.Controls.Add(newTextBox);
                //开启按钮
                Button newButton = new Button();
                newButton.Name = value[0]+"Start";
                newButton.Text = value[0]+"开启";
                newButton.Width = 90;
                newButton.Height = 32;
                Point newLocation = new Point(x, y);
                newButton.Location = newLocation;
                StartAndStop startAndStop = new StartAndStop(tabControl, newTabPage, newTextBox, value);
                newButton.Click += new EventHandler(startAndStop.Button_Click_Start);
                form.Controls.Add(newButton);
                //关闭按钮
                Button newButton1 = new Button();
                newButton1.Name = value[0]+"Stop";
                newButton1.Text = value[0]+"关闭";
                newButton1.Width = 90;
                newButton1.Height = 32;
                Point newLocation1 = new Point(x+90, y);
                newButton1.Location = newLocation1;
                newButton1.Click += new EventHandler(startAndStop.Button_Click_Stop);
                form.Controls.Add(newButton1);
                y = y + 40;
               
            }
        }

    }
}
